package ifi.p17.jarmanagement.model;

import ifi.p17.jarmanagement.MainActivity;
import ifi.p17.jarmanagement.R;
import ifi.p17.jarmanagement.control.FileProcess;
import ifi.p17.jarmanagement.control.TimeDate;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Jar {
	
	public final static int NEC_DEFAULT = 55;
	public final static int FFA_DEFAULT = 10;
	public final static int LTSS_DEFAULT = 10;
	public final static int EDU_DEFAULT = 10;
	public final static int PLAY_DEFAULT = 10;
	public final static int GIVE_DEFAULT = 5;
	
	MainActivity atv;
	Revenue revenu;
	String name;
	int total; 
	int current;
	int percent;
	TextView tv_jar, tv_proc;
	String unit;
	Purchase cur_purchase;
	
	public Jar(MainActivity atv, String name, Revenue revenu, Purchase pur) {
		this.atv = atv;
		this.name = name;
		this.current = 0 ;
		this.revenu = revenu;
		this.unit = revenu.getUnit();
		this.cur_purchase = pur;
	}

	public String getTextInUnit(){
		return Integer.toString(current) + "/" + Integer.toString(total) + " ("+unit+")";
	}
	
	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public String getName() {
		return name;
	}

	public void addCurrent(int v){
		this.current += v;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public int getPercent() {
		return percent;
	}

	public void setPercent(int percent) {
		this.percent = percent;
	}

	public TextView getTv_jar() {
		return tv_jar;
	}

	public void setTv_jar(TextView tv_jar) {
		this.tv_jar = tv_jar;
		controlTVJar();
	}

	public TextView getTv_proc() {
		return tv_proc;
	}

	public void setTv_proc(TextView tv_proc) {
		this.tv_proc = tv_proc;
		controlTVProc();
	}

	private void updateTVJar(){
		tv_jar.setText(Integer.toString(percent) + "%");
	}
	
	private void updateTVProc(){
		tv_proc.setText(getTextInUnit());
		
		// update background
		double p = current*100.0 / total;
//		LogErorr.out(Double.toString(p));
		
		if (p <= 2) tv_proc.setBackgroundResource(R.drawable.bar_boder);
		else if (p<=10) tv_proc.setBackgroundResource(R.drawable.bar_10);
		else if (p<=20) tv_proc.setBackgroundResource(R.drawable.bar_20);
		else if (p<=30) tv_proc.setBackgroundResource(R.drawable.bar_30);
		else if (p<=40) tv_proc.setBackgroundResource(R.drawable.bar_40);
		else if (p<=50) tv_proc.setBackgroundResource(R.drawable.bar_50);
		else if (p<=60) tv_proc.setBackgroundResource(R.drawable.bar_60);
		else if (p<=70) tv_proc.setBackgroundResource(R.drawable.bar_70);
		else if (p<=80) tv_proc.setBackgroundResource(R.drawable.bar_80);
		else if (p<=90) tv_proc.setBackgroundResource(R.drawable.bar_90);
		else if (p<=100) tv_proc.setBackgroundResource(R.drawable.bar_100);
	}
	
	public void updateJar(){
		updateTVJar();
		updateTVProc();
	}
	
	private void controlTVJar() {
		
	}

	private void controlTVProc() {
		tv_proc.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				longClickAction(v);
				return false;
			}
		});
	}


protected void longClickAction(View v) {
	LayoutInflater layoutInflater = LayoutInflater.from(v.getContext());
	
	View promptView = layoutInflater.inflate(R.layout.jar_input, null);

	AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(v.getContext());

	// set prompts.xml to be the layout file of the alertdialog builder
	alertDialogBuilder.setView(promptView);

	
	final TextView cur_time = (TextView) promptView.findViewById(R.id.jar_input_cur_time);
	final EditText input = (EditText) promptView.findViewById(R.id.jar_input_purchase);
	final EditText description = (EditText) promptView.findViewById(R.id.jar_input_purchase_des);

	final TimeDate timedate = new TimeDate();
	cur_time.setText(timedate.getHMS());
	
	// setup a dialog window

	alertDialogBuilder
			.setCancelable(false)
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							// get user input and set it to result
							String in = input.getText().toString();
							
							if (in!=null && in.length()>0){
								int pur = Integer.parseInt(in);
								String des = description.getText().toString();
								updateNewPurchase(timedate, pur, des);
							}
						}
					})
			.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,	int id) {
							dialog.cancel();
						}
					});

	// create an alert dialog
	AlertDialog alertD = alertDialogBuilder.create();

	alertD.show();

}

protected void updateNewPurchase(TimeDate timedate, int pur, String des) {
	// validate pur
	if (current+pur > total){
		// out of total
		AlertDialog.Builder builder = new AlertDialog.Builder(tv_proc.getContext());
		
		builder.setMessage("Out of JAR")
				.setTitle("Error");
		
		builder
			.setCancelable(false)
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
		
		AlertDialog alert = builder.create();
		alert.show();
	} else {
		// update current purchase
		cur_purchase.setNormalTypeInfo(name, timedate.toString(), pur, unit, des);
//		cur_purchase.setType(Purchase.TYPE_NORMAL);
//		cur_purchase.setJar(name);
//		cur_purchase.setTime(timedate.toString());
//		cur_purchase.setValue(pur);
//		cur_purchase.setUnit(unit);
//		cur_purchase.setDescription(des);
		
		// update revenu textview
		revenu.setRemaining(revenu.getRemaining() - pur);
//		revenu.updateText();
		
		// update jar
		current += pur;
//		updateTVProc();
		
		// save to file
		String data = name + "@" + timedate.toString() + "@" + Integer.toString(pur) + "@" + unit + "@" + des;
		String folder = timedate.getYear() + "/" + timedate.getMonth();
		String filename = timedate.getDay();
		FileProcess.writeToFile(data, folder, filename, true);
		
		String data2 = Integer.toString(revenu.getAmount()) + " " + Integer.toString(revenu.getRemaining());
		FileProcess.writeToFile(data2, folder, FileProcess.REMAINING_FILE, false);
		
		atv.drawWindow();
	}
	
}

//protected void updateJar(int purchase) {
//	current += purchase;
//	textImage.setText(getTextInUnit());
//}


}
